﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using MongoDB.Bson;
using MongoDB.Driver;
using MongoDB.Driver.Builders;

namespace mongodb
{
    internal class Program
    {
        //IBaseDAL dal = new IBaseDAL();
        private static void Main(string[] args)
        {
            #region  基础写法

            //数据库连接字符串
            const string connectionString = "mongodb://127.0.0.1:27017";
            //数据库
            const string Database = "student";

            MongoClient client = new MongoClient(connectionString);
            MongoServer server = client.GetServer();
            MongoDatabase db = server.GetDatabase(Database);
            MongoCollection collection = db.GetCollection("student");

            //            //新增
            //student stud_Add = new student() { name = "赵云", code = "2001", sex = "女", age = "23" };
            //collection.Insert<student>(stud_Add);

            //            //更新
            //            //QueryDocument query_upd = new QueryDocument { { "name", "赵云" } };
            //            //IMongoUpdate update = Update.Set("code", "4444");
            //            //collection.Update(query_upd, update);

            //            //删除
            //            //QueryDocument query_del = new QueryDocument { { "name", "赵云" } };
            //            //collection.Remove(query_del);
            //查询
            //QueryDocument query_sel = new QueryDocument { { "name", "赵云" } };
            //var list = collection.FindAs<student>(query_sel);
            //foreach (student item in list)
            //{
            //    Console.WriteLine("name：{0},code:{1}", item.name, item.code + "id:" + item._id);
            //}
            //}

            #endregion

            //student stud_Add2 = new student() { name = "小红", code = "2001", sex = "女", age = "23" };

            //bool dd = DoMongo.Insert("student", stud_Add2);
            QueryDocument update = new QueryDocument() { { "name", "22222" } };
             DoMongo.Update("student", Query<student>.EQ(s => s.name, "小红"), update);
             List<student> student = DoMongo.GetList<student>("student", Query<student>.EQ(s => s.name, "22222"));
          
            //long num = DoMongo.Count("student");
            //long rows = 0;
            //List<student> student1 = DoMongo.GetPageList<student>("student", Query<student>.EQ(s => s.name, "小红"), "name", true, 1, 5, out  rows);

        }

    }



    public class student
    {
        public ObjectId _id; //BsonType.ObjectId 这个对应了 MongoDB.Bson.ObjectId 
        public string name { get; set; }
        public string code { get; set; }
        public string sex { get; set; }
        public string age { get; set; }
    }
}
